

import 'dart:typed_data';

import 'package:bookshelf/main.dart';

import 'convert_util.dart';

String? analysisReaderRule(String uid){
  String rule = settingEntity.analysisReader??'xy';
  String? result = '';
  if(rule == 'xy'){
    result = xyRule(uid);
  }
  if(rule == 'py'){
    result = pyRule(uid);
  }
  return result;
}

String? xyRule(String uid){
  if(uid.length >= 8){
    String exUid = uid.substring(6,8)+uid.substring(4,6)+uid.substring(2,4)+uid.substring(0,2);
    String uidNo = hexToInt(exUid).toString();
    while(uidNo.length<10){
      uidNo = '0$uidNo';
    }
    return uidNo;
  }
  return null;
}

String? pyRule(String uid){
  if(uid.length >= 26){
    List<int> uidList = [];
    int length = uid.length;
    int i = 0;
    while(i<length){
      var x = int.parse("0x${uid.substring(i,i+2)}");
      uidList.add(x);
      i = i+2;
    }
    Uint8List bytes = Uint8List.fromList(uidList);
    String asciiString = String.fromCharCodes(bytes);
    if(asciiString.length>13){
      asciiString = asciiString.substring(0,13);
    }
    return asciiString;
  }
  return null;
}
